MapService.Control.MeasureArea = OpenLayers.Class(OpenLayers.Control.Measure, {
	type           : OpenLayers.Control.TYPE_TOOL,
	persist        : true,
	immediate      : true,
	geodesic       : true,
	textNodes      : null,
	pointNodes     : null,
	
	initialize     : function(options){
		var me = this;
		
		me.textNodes  = [];
		me.pointNodes = [];
		
		var pointStyle = {
			pointRadius        : 4,
			graphicName        : "square",
			fillColor          : "white",
			fillOpacity        : 1,
			strokeWidth        : 1,
			strokeOpacity      : 1,
			strokeColor        : "#333333",
			fontColor          : "#FF0000",
			fontSize           : "11px",
			fontFamily         : "Arial",
			fontWeight         : "bold",
			labelAlign         : "lm",
			labelOutlineWidth  : 1,
			labelOutlineColor  : "#FFFFFF"
		};
		
		var lineStyle = {
			strokeWidth   : 3,
			strokeOpacity : 1,
			strokeColor   : "#666666"
		};
		
		var polygonStyle = {
			strokeWidth    : 2,
			strokeOpacity  : 1,
			strokeColor    : "#666666",
			fillColor      : "white",
			fillOpacity    : 0.3
		};
		
		var rule = new OpenLayers.Rule({
        	symbolizer: {
        		"Point"   : pointStyle,
        		"Line"    : lineStyle,
        		"Polygon" : polygonStyle
        	}
        });
		
		var style = new OpenLayers.Style(null, {
			rules: [rule]
		});
			
		me.handlerOptions = {
			layerOptions : {
				styleMap: new OpenLayers.StyleMap({
					"default": style
				})
			}
		};
		
		
		OpenLayers.Control.Measure.prototype.initialize.apply(this, [OpenLayers.Handler.Polygon, options]);
		
		me.displayClass = 'ux-icon-measure-area';
		me.events.register('measurepartial', null, me.onMeasurePartial);
	},
	
	onMeasurePartial : function(evt){
		var me = this;
		
		var geometry = evt.geometry;
		var total = me.getBestArea(geometry);
		var polygonStyle = {
			strokeWidth         : 2,
			strokeOpacity       : 1,
			strokeColor         : "#666666",
			fillColor           : "white",
			fillOpacity         : 0.3,
			fontColor           : "#FF0000",
			fontSize            : "11px",
			fontFamily          : "Arial",
			fontWeight          : "bold",
			labelAlign          : "lt",
			labelOutlineWidth   : 1,
			labelOutlineColor   : "#FFFFFF", 
			label               : total[0].toFixed(2) + " " + total[1] + '²'
		};
		me.handler.polygon.style = polygonStyle;

		var geometry = evt.geometry.components[0];
		if(me.textNodes.length >= geometry.components.length || total[0] == 0){
			me.handler.layer.removeAllFeatures();
			me.textNodes = [];
			me.pointNodes = [];
		};
		
		if(geometry.components.length-me.pointNodes.length >= 2){
			var c = geometry.components[geometry.components.length-2];
			var pointNodeStyle = {
				pointRadius     : 4,
				graphicName     : "square",
				fillColor       : "white",
				fillOpacity     : 1,
				strokeWidth     : 1,
				strokeOpacity   : 1,
				strokeColor     : "#333333"					
			};
			var pointNode = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(c.x, c.y),{},pointNodeStyle);
			me.pointNodes.push(pointNode);
            me.handler.layer.addFeatures([pointNode]);
		};
		
		//Primeira linha
		var fromLen = 3;
		var toLen = 2;
		var textNodeLen = 3;
		
		for(var i = 1; i <= 2; i++){
			var len = geometry.components.length;
			var from = geometry.components[len - fromLen];
			var to = geometry.components[len - toLen];
            var ls = new OpenLayers.Geometry.LineString([from, to]);
            var dist = me.getBestLength(ls);
            if (!dist[0]) {
                return;
            }
            var label = dist[0].toFixed(2) + " " + dist[1];
            var textNode = me.textNodes[len - textNodeLen] || null;
            if (textNode && !textNode.layer) {
                me.textNodes.pop();
                textNode = null;
            }
            if (!textNode) {
                var c = ls.getCentroid();
                var textNodeStyle = {
                	fontColor           : "#000000",
                	fontSize            : "10px",
                	fontFamily          : "Arial",
                	labelAlign          : "cm",
                	labelOutlineWidth   : 2,
                	labelOutlineColor   : "#FFFFFF"
                };
                textNode = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(c.x, c.y),{},textNodeStyle);
                me.textNodes.push(textNode);
                me.handler.layer.addFeatures([textNode]);
            }
            textNode.geometry.x = (from.x + to.x) / 2;
            textNode.geometry.y = (from.y + to.y) / 2;
            textNode.style = textNode.style || {};
            textNode.style.label = label;
            textNode.layer.drawFeature(textNode);
		}
	},
	
	CLASS_NAME: "MapService.Control.MeasureArea"
});